
package com.xssweibo.base;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.R.integer;
import android.media.audiofx.Visualizer;
import android.util.JsonToken;

import com.weibo.sdk.android.Weibo;

/*
数据格式定义
http://open.weibo.com/wiki/%E5%B8%B8%E8%A7%81%E8%BF%94%E5%9B%9E%E5%AF%B9%E8%B1%A1%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84?sudaref=open.weibo.com
*/

/**
 * A data class representing one single status of a user.
 * @author lwq
 */
public class Status implements java.io.Serializable {

    private String createdAt;
    private long id;
    private long mid;
    private String text;
    private String source;
    private boolean favorited;
    private boolean truncated;
    private String inReplyToStatusId;
    private String inReplyToUserId;
    private String inReplyToScreenName;
    private String thumbnailPic;
    private String bmiddlePic;
    private String originalPic;
    private Geo geo; 		// 地理信息字段
    private User user;		// 微博作者的用户信息字段
    private RetweetDetails retweetedStatus; 	
    private int repostsCount;
    private int commentsCount;
    private int attitudesCount;
    private int mlevel;
    private String visible;
    private static final long serialVersionUID = 1608000492860584608L;

    
    /* modify by sycheng add some field*/
    public Status(JSONObject json)throws WeiboException, JSONException{
    	init(json);
    }
    public Status(String str) throws WeiboException, JSONException {
        // StatusStream uses this constructor
        JSONObject json = new JSONObject(str);
        init(json);
    }

    private void init(JSONObject json) throws
            WeiboException, JSONException {
    	createdAt = json.getString("created_at");
    	id = json.getLong("id");
    	mid = json.getLong("mid");
        text = json.getString("text");
        source = json.getString("source");
        favorited = json.getBoolean("favorited");
        truncated = json.getBoolean("truncated");
        
        inReplyToStatusId = json.getString("in_reply_to_status_id");
        inReplyToUserId = json.getString("in_reply_to_user_id");
        inReplyToScreenName = json.getString("in_reply_to_screen_name");
        
		if (!json.isNull("thumbnail_pic"))
			thumbnailPic = json.getString("thumbnail_pic");
		if (!json.isNull("bmiddle_pic"))
			bmiddlePic = json.getString("bmiddle_pic");
		if (!json.isNull("original_pic"))
			originalPic = json.getString("original_pic");
  
		if (!json.isNull("geo"))
			geo = new Geo(json.getJSONObject("geo"));
		if (!json.isNull("original_pic"))
			user = new User(json.getJSONObject("user"));
        
        repostsCount = json.getInt("reposts_count");
        commentsCount = json.getInt("comments_count");
        attitudesCount = json.getInt("attitudes_count");
        mlevel = json.getInt("mlevel");
        if (!json.isNull("visible"))
        	visible = json.getString("visible");
        
        if(!json.isNull("retweeted_status")){
        	retweetedStatus = new RetweetDetails(json.getJSONObject("retweeted_status"));
		}
    }

    /**
     * Return the created_at
     *
     * @return created_at
     */

    public String getCreatedAt() {
        return this.createdAt;
    }

    /**
     * Returns the id of the status
     *
     * @return the id
     */
    public long getId() {
        return this.id;
    }

    /**
     * Returns the text of the status
     *
     * @return the text
     */
    public String getText() {
        return this.text;
    }

    /**
     * Returns the source
     *
     * @return the source
     * @since Weibo4J 1.0.4
     */
    public String getSource() {
        return this.source;
    }


    /**
     * Test if the status is truncated
     *
     * @return true if truncated
     * @since Weibo4J 1.0.4
     */
    public boolean isTruncated() {
        return truncated;
    }

    /**
     * Returns the in_reply_tostatus_id
     *
     * @return the in_reply_tostatus_id
     * @since Weibo4J 1.0.4
     */
    public String getInReplyToStatusId() {
        return inReplyToStatusId;
    }

    /**
     * Returns the in_reply_user_id
     *
     * @return the in_reply_tostatus_id
     * @since Weibo4J 1.0.4
     */
    public String getInReplyToUserId() {
        return inReplyToUserId;
    }

    /**
     * Returns the in_reply_to_screen_name
     *
     * @return the in_in_reply_to_screen_name
     * @since Weibo4J 2.0.4
     */
    public String getInReplyToScreenName() {
        return inReplyToScreenName;
    }

    /**
     * 返回地理信息字段.
     *
     * @return the geo
     */
    public Geo getGeo() {
        return geo;
    }

    /**
     * Test if the status is favorited
     *
     * @return true if favorited
     * @since Weibo4J 1.0.4
     */
    public boolean isFavorited() {
        return favorited;
    }

    public String getThumbnail_pic() {
		return thumbnailPic;
	}

	public String getBmiddle_pic() {
		return bmiddlePic;
	}

	public String getOriginal_pic() {
		return originalPic;
	}

    /**
     * Return the user
     *
     * @return the user
     */
    public User getUser() {
        return user;
    }

    /**
     *
     * @since Weibo4J 2.0.10
     */
    public boolean isRetweet(){
        return null != retweetedStatus;
    }

    public RetweetDetails getRetweetDetails() {
        return retweetedStatus;
    }

    /*modify by sycheng add json call method*/
    /*package*/
    static public List<Status> constructStatuses(String res) throws WeiboException {
    	 try {
    		 JSONObject object = new JSONObject(res);
             JSONArray list = object.getJSONArray("statuses");
             int size = list.length();
             List<Status> statuses = new ArrayList<Status>(size);
            
             for (int i = 0; i < size; i++) {
                 statuses.add(new Status(list.getJSONObject(i)));
             }
             return statuses;
         } catch (JSONException jsone) {
             throw new WeiboException(jsone);
         } catch (WeiboException te) {
             throw te;
         }

    }
    	 

    @Override
    public int hashCode() {
        return (int) id;
    }

    @Override
    public boolean equals(Object obj) {
        if (null == obj) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        return obj instanceof Status && ((Status) obj).id == this.id;
    }

    @Override
    public String toString() {
        return "Status{" +
                "createdAt=" + createdAt +
                ", id=" + id +
                ", text='" + text + '\'' +
                ", source='" + source + '\'' +
                ", isTruncated=" + truncated +
                ", inReplyToStatusId=" + inReplyToStatusId +
                ", inReplyToUserId=" + inReplyToUserId +
                ", isFavorited=" + favorited +
                ", thumbnail_pic=" + thumbnailPic +
                ", bmiddle_pic=" + bmiddlePic +
                ", original_pic=" + originalPic +
                ", inReplyToScreenName='" + inReplyToScreenName + '\'' +
                ", latitude=" + "" +
                ", longitude=" + "" +
                ", retweetDetails=" + retweetedStatus +
                ", user=" + user +
                '}';
    }
}
